package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Jwts;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.BrandMapper;
import com.bwie.pojo.Brand;
import com.bwie.service.BrandService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService{
    @Autowired
    BrandMapper brandMapper;

    @Autowired
    HttpServletRequest request;

    @Override
    public R getBrandList(PageInfoVo pageInfoVo) {
        Page<Brand> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        //品牌名称模糊
        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            wrapper.lambda().like(Brand::getBrandName,pageInfoVo.getKeyWord());
        }
        wrapper.lambda().orderByDesc(Brand::getCreateTime);
        Page<Brand> brandPage = brandMapper.selectPage(page, wrapper);
        return R.ok().setData(brandPage);
    }

    @Override
    public R addBrand(Brand brand) {
        Brand brand1 = brandMapper.selectOne(new QueryWrapper<Brand>().lambda().eq(Brand::getBrandName, brand.getBrandName()));
        if(ObjectUtils.isNotEmpty(brand1)){
            return R.error().setMsg("名称不能重复");
        }

        String token = request.getHeader("token");
        if(token==null){
            return R.error().setMsg("请先登录");
        }
        String userName = Jwts.parser().setSigningKey("bwie").parseClaimsJws(token).getBody().get("userName", String.class);

        brand.setCreateBy(userName);
        brand.setCreateTime(new Date());

        brandMapper.insert(brand);
        return R.ok();
    }

    @Override
    public R changeBrandStatus(Brand brand) {
        Brand brand1 = brandMapper.selectById(brand.getBrandId());
        if(brand1==null){
            return R.error().setMsg("品牌不存在");
        }

        //修改状态
        String token = request.getHeader("token");
        if(token==null){
            return R.error().setMsg("请先登录");
        }
        String userName = Jwts.parser().setSigningKey("bwie").parseClaimsJws(token).getBody().get("userName", String.class);

        brand.setUpdateBy(userName);
        brand.setUpdateTime(new Date());

        brand.setBrandStatus(brand.getBrandStatus());
        brandMapper.updateById(brand);
        return R.ok();
    }

    @Override
    public R deleteBrand(Integer brandId) {
        Brand brand = brandMapper.selectById(brandId);
        if(brand==null){
            return R.error().setMsg("品牌不存在");
        }
        brandMapper.deleteById(brandId);
        return R.ok();
    }

    @Override
    public R deletebatchBrand(List<Brand> brandList) {
//        for (Brand brand : brandList) {
//            brandMapper.deleteById(brand.getBrandId());
//        }
        List<Integer> collect = brandList.stream().map(item -> item.getBrandId()).collect(Collectors.toList());
        brandMapper.deleteBatchIds(collect);
        return R.ok();
    }


}
